<?php

class UserAction extends Action{


 function user_list() {

           if(!empty($_POST['sub'])){

           	$_POST["locked"]==''          ? $locked =''        :$locked =$_POST['locked'];
		   	$_POST["username"]==''        ? $username =''      :$username =$_POST['username'];
            $_POST["inp"]==''             ? $inp =''           :$inp  =  $_POST['inp'];
		   	$_SESSION['user_where']='';

		   	if($inp!=''){

		   		$field = $_POST['target'];
		   		if($field=='username'){
		   			 $_SESSION['user_where'].= ' and user.'.$field.' like "%'.$inp.'%" ';
		   		}else{
		   			 $_SESSION['user_where'].= ' and msg.'.$field.' like "%'.$inp.'%" ';
		   		}

		   	}
		   	if($locked!=''){
		   		$_SESSION['user_where'].= ' and locked = '.$locked;
		   	}
           }

           import('@.ORG.Page');

		   $list         =   D('User');
		   $count        =   $list->table(array("gdqy_user"=>"user","gdqy_user_msg"=>'msg'))->where('friends.user_id=user.user_id = msg.user_id'.$_SESSION['user_where'])->count();
		   $page         =   new Page($count,10);
		   $dis_pag      =   $page->show();

           $list_u         =  $list->table(array("gdqy_user"=>"user","gdqy_user_msg"=>'msg','gdqy_user_friends'=>'friends'))->where('friends.user_id=user.user_id = msg.user_id'.$_SESSION['user_where'])->limit($page->firstRow.','.$page->listRows)->select();
           $this->assign('page',$dis_pag);
           $this->assign('list_u',$list_u);
           $this->display();

    }
 function locked_user(){

 	    if($_GET['id']){
	        $list       =   D('User');
	        $data['user_id'] =   $_GET['id'];
	        $data['locked'] =   $_GET['l'];
	        $list->data($data)->save();
	        	echo '<script type="text/javascript">alert("success");location.href="user_list"</script>';
	  	  }

   }
 function del_user(){

 	   if($_GET['id']){
 	   	$id         =   $_GET['id'];
 	   	$list       =   D('User');
 	   	$friend       =   D('User_friends');
 	   	$img        =$list->relation(true)->where('user_id ='.$id)->select();
 	   	$arr_firends= explode('|',$img[0]['befocus_ids']);
 	   	foreach($arr_firends as $value){

 	   		$befocus_field= $list->relation(true)->where('user_id = '.$value)->select();
 	   		$arr_focus = explode('|',$befocus_field[0]['focus_ids']);
 	   		dump($arr_focus);
           for( $i=0;$i<count($arr_focus); $i++)
			{
			 if( $arr_focus[$i] == $id )
			 {
			  unset( $arr_focus[$i] );
			 }

 	   	   }
           $data['focus_ids'] = implode("|",$arr_focus);
           $data['id']        = $befocus_field[0]['id'];
           $friend->data($data)->save();
 	   	 }

 	   	if(file_exists('./user_img/'.$img[0]['image'])){
 	   		  unlink('./user_img/'.$img[0]['image']);
 	   	}

 	   	$list_m       =   D('Msg');
 	   	$user_m     =   $list_m->field('id')->where('author_id='.$id)->select();

	   	$list_c       =   D('Comm');
        foreach($user_m as $val){
        	$list_c->where('mes_id = '.$val['id'])->delete();
        }
            $list_m->where('author_id='.$id)->delete();
            $list->relation(true)->delete($id);

 	   	echo '<script type="text/javascript">alert("success");location.href="user_list"</script>';

 	   }

 }
 function user_detail(){
       $list         =   D('User');
       $list_u       =  $list->relation(true)->where('user_id ='.$_GET['id'])->select();

       foreach($list_u[0] as $key=>$val){
       	 $user_detail[$key] = $val;
       }

       $this->assign('detail',$user_detail);
       $this->display();

 }

 function check(){

 	   $che        =   $_POST['checkbox'];

 	   if($_POST['all_locked_sub']){
 	   	   $list       =   D('User');
	 	   foreach($che as $value){

	 	   	$data['user_id'] =   $value;
	        $data['locked'] =   $_POST['all_locked'];
	        $list->data($data)->save();
	        	echo '<script type="text/javascript">alert("success");location.href="user_list"</script>';
	  	  }


 	   }
 	   if($_POST['all_del_sub']){

	 	   $list       =   D('User');
	       $list_m       =   D('Msg');
	       $list_c       =   D('Comm');
	 	   foreach($che as $value){
		 	   	$img        =$list->relation(true)->where('user_id ='.$value)->select();
		 	   	if(file_exists('./user_img/'.$img[0]['image'])){
		 	   		  unlink('./user_img/'.$img[0]['image']);
		 	   	}


		 	   	$user_m     =   $list_m->field('id')->where('author_id='.$value)->select();


		        foreach($user_m as $val){
		        	$list_c->where('mes_id = '.$val['id'])->delete();
		        }
		            $list_m->where('author_id='.$value)->delete();
		            $list->relation(true)->delete($value);

	 	   }
	 	   echo '<script type="text/javascript">alert("success");location.href="user_list"</script>';

 	   }
 }
 function focus_user(){

    	if(!empty($_GET['focus'])){
    	   $arr_focus  = explode('|',$_GET['focus']);
           $arr_list   = $this->select_friends($arr_focus);

           $this->assign('arr_list',$arr_list);
           $this->assign('title','他关注的人');

    	}elseif(!empty($_GET['befocus'])){

    	  $arr_focus  = explode('|',$_GET['befocus']);
    	  $arr_list   = $this->select_friends($arr_focus);
          $this->assign('arr_list',$arr_list);
          $this->assign('title','关注他的人');
    	}
    	$this->display();
 }
 private function select_friends($arr_focus){
 	    $list       =   D('User');
        foreach($arr_focus as $val){
               $arr[]=$list->relation(true)->where('user_id ='.$val)->select();
           }
           for($i=0;$i<count($arr);$i++){
             $arr_list[] = $arr[$i][0];
           }
           return $arr_list ;
 }

}
?>